<?php
 /**
 *  +---------------------------------------------------------------------- 
 *  | 文件功能：程序数据排序功能工具集合
 *  +---------------------------------------------------------------------- 
 *  | Copyright (c) 2022~2024 https://www.huihuiv.com All rights reserved.
 *  +---------------------------------------------------------------------- 
 *  | Create At: 2024年02月26日 18:09:46
 *  +---------------------------------------------------------------------- 
 *  | Author: 半壶水 <star@cqshanwen.com>
 *  +---------------------------------------------------------------------- 
 */

namespace App\Librarys;

class Sort
{
    /**
     * 生成权限结构树
     *
     * @param integer $pid
     * @param mixed $lists
     * @return array
     */
    public static function build_auth_tree($pid = 0, mixed $lists): array
    {
        $list = [];
        foreach ($lists as $key => $val) {
            if ($pid == $val->pid) {
                $node = $val;
                $children = self::build_auth_tree($val->auth_id, $lists);
                if (!empty($children)) {
                    $node->children = $children;
                }
                $list[] = $node;
            }
        }
        return $list;
    }

    public static function build_category_tree($pid = 0, mixed $lists): array
    {
        $list = [];
        foreach ($lists as $key => $val) {
            if ($pid == $val->pid) {
                $node = $val;
                $children = self::build_category_tree($val->cid, $lists);
                if (!empty($children)) {
                    $node->children = $children;
                }
                $list[] = $node;
            }
        }
        return $list;
    }

    public static function build_auth_select_tree($pid = 0, mixed $lists): array
    {
        $list = [];
        foreach ($lists as $key => $val) {
            if ($pid == $val->pid) {
                $node = $val;
                $node->label = $val->title;
                $node->value = $val->auth_id;
                $children = self::build_auth_tree($val->auth_id, $lists);
                if (!empty($children)) {
                    $node->children = $children;
                }
                $list[] = $node;
            }
        }
        return $list;
    }
}
